package com.example.swimfish;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.Group;
import javafx.scene.effect.BlendMode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.util.Duration;

public class Aquarium2DView extends Group {
    private static final double AQUARIUM_WIDTH = 800;
    private static final double AQUARIUM_HEIGHT = 600;

    private final Group aquarium = new Group();
    private final Group fishes = new Group();

    public Aquarium2DView() {
        createAquarium();
        this.getChildren().addAll(aquarium, fishes);
    }

    private void createAquarium() {
        // 创建水底渐变背景 - 从浅蓝到深蓝
        LinearGradient waterGradient = new LinearGradient(
                0, 0, 0, 1,
                true, CycleMethod.NO_CYCLE,
                new Stop(0, Color.rgb(0, 150, 255)),     // 顶部较浅
                new Stop(0.5, Color.rgb(0, 100, 200)),  // 中间中等
                new Stop(1, Color.rgb(0, 50, 100))      // 底部较深
        );

        Rectangle background = new Rectangle(AQUARIUM_WIDTH, AQUARIUM_HEIGHT);
        background.setFill(waterGradient);

        // 创建水底沙地效果
        Rectangle bottom = new Rectangle(AQUARIUM_WIDTH, 60);
        bottom.setFill(Color.rgb(210, 180, 140));  // 沙色
        bottom.setY(AQUARIUM_HEIGHT - 60);

        // 添加一些水底细节（气泡、水草等）
        addWaterDetails();

//        aquarium.getChildren().addAll(background, bottom);

        // 添加水面光效动画
        Rectangle lightEffect = new Rectangle(AQUARIUM_WIDTH, AQUARIUM_HEIGHT / 2);
        lightEffect.setFill(new LinearGradient(
                0, 0, 0, 1,
                true, CycleMethod.NO_CYCLE,
                new Stop(0, Color.rgb(255, 255, 255, 0.1)),
                new Stop(1, Color.TRANSPARENT)
        ));
        lightEffect.setBlendMode(BlendMode.OVERLAY);
        lightEffect.setY(0);

        // 创建水面波动动画
        Timeline waveAnimation = new Timeline(
                new KeyFrame(Duration.ZERO,
                        new KeyValue(lightEffect.opacityProperty(), 0.1),
                        new KeyValue(lightEffect.scaleYProperty(), 1.0)
                ),
                new KeyFrame(Duration.seconds(2),
                        new KeyValue(lightEffect.opacityProperty(), 0.15),
                        new KeyValue(lightEffect.scaleYProperty(), 1.05)
                ),
                new KeyFrame(Duration.seconds(4),
                        new KeyValue(lightEffect.opacityProperty(), 0.1),
                        new KeyValue(lightEffect.scaleYProperty(), 1.0)
                )
        );
        waveAnimation.setCycleCount(Animation.INDEFINITE);
        waveAnimation.setAutoReverse(true);
        waveAnimation.play();

        aquarium.getChildren().addAll(background, bottom, lightEffect);

        // 添加气泡上升动画
        addBubbleAnimation();
    }

    private void addWaterDetails() {
        // 添加气泡效果
        for (int i = 0; i < 30; i++) {
            Rectangle bubble = new Rectangle(
                    Math.random() * AQUARIUM_WIDTH,
                    Math.random() * AQUARIUM_HEIGHT,
                    2 + Math.random() * 5, 2 + Math.random() * 5
            );
            bubble.setFill(Color.rgb(255, 255, 255, 0.5));
            aquarium.getChildren().add(bubble);
        }

        // 添加水草效果
        for (int i = 0; i < 10; i++) {
            double x = Math.random() * AQUARIUM_WIDTH;
            double height = 30 + Math.random() * 70;

            LinearGradient seaweedGradient = new LinearGradient(
                    0, 0, 0, 1,
                    true, CycleMethod.NO_CYCLE,
                    new Stop(0, Color.TRANSPARENT),
                    new Stop(0.3, Color.rgb(0, 100, 0, 0.7)),
                    new Stop(1, Color.rgb(0, 60, 0, 0.9))
            );

            Rectangle seaweed = new Rectangle(
                    x, AQUARIUM_HEIGHT - height,
                    5 + Math.random() * 10, height
            );
            seaweed.setFill(seaweedGradient);
            aquarium.getChildren().add(seaweed);
        }
    }
    private void addBubbleAnimation() {
        for (int i = 0; i < 20; i++) {
            Circle bubble = new Circle(
                    Math.random() * AQUARIUM_WIDTH,
                    AQUARIUM_HEIGHT - 20,
                    2 + Math.random() * 4
            );
            bubble.setFill(Color.rgb(255, 255, 255, 0.3 + Math.random() * 0.3));

            Timeline bubbleRise = new Timeline(
                    new KeyFrame(Duration.ZERO,
                            new KeyValue(bubble.centerYProperty(), bubble.getCenterY()),
                            new KeyValue(bubble.opacityProperty(), 0.7)
                    ),
                    new KeyFrame(Duration.seconds(3 + Math.random() * 4),
                            new KeyValue(bubble.centerYProperty(), -20),
                            new KeyValue(bubble.opacityProperty(), 0)
                    )
            );
            bubbleRise.setCycleCount(Animation.INDEFINITE);
            bubbleRise.setDelay(Duration.seconds(Math.random() * 5));
            bubbleRise.play();

            aquarium.getChildren().add(bubble);
        }
    }

    public void addFish(AnimatedFishModel fish) {
//        fishes.getChildren().add(fish);
//        fish.startSwimming();
        javafx.application.Platform.runLater(() -> {
            fishes.getChildren().add(fish);
            fish.startSwimming();
        });
    }

    public void removeFish(AnimatedFishModel fish) {
        fishes.getChildren().remove(fish);
        fish.stopSwimming();
    }

    public double getWidth() {
        return AQUARIUM_WIDTH;
    }

    public double getHeight() {
        return AQUARIUM_HEIGHT;
    }
}

